package com.interfaces;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.web.CommonJspKeys;
import com.common.web.Json;
import com.common.web.ReturnCodes;
import com.core.sysConfig.bo.SysConfigBo;
import com.core.sysConfig.dto.SysConfigSearchCondition;
import com.core.sysConfig.pojo.SysConfig;
import com.core.user.pojo.User;
import com.util.DataBaseUtil;
import com.util.GsonUtil;
import com.util.exception.ComRuntimeException;
import com.util.exception.ComSystemException;
@Controller
public class SysConfigController {
	protected Logger log = Logger.getLogger(SysConfigController.class);
	@Resource
	private SysConfigBo sysConfigBo;
	public static final String URL_ENTITY = "/config";
    public static final String URL_GOTO_MAIN_PAGE = URL_ENTITY + "/goto_main_page";
    public static final String URL_GOTO_ADD_UPDATE_INDEX_PAGE = URL_ENTITY + "/goto_add_update_index_page";
    public static final String URL_GOTO_ADD_UPDATE_PAGE = URL_ENTITY + "/goto_add_update_page";
    public static final String URL_GOTO_ADD_UPDATE_ROLE_PAGE = URL_ENTITY + "/goto_add_update_role_page";
    public static final String URL_GOTO_ADD_PAGE = URL_ENTITY + "/goto_add_page";
    public static final String URL_GOTO_UPDATE_PAGE = URL_ENTITY + "/goto_update_page";
    public static final String URL_GOTO_UPDATE_PASSWORD_PAGE = URL_ENTITY + "/goto_update_password_page";

    public static final String URL_ADD = URL_ENTITY + "/add";
    public static final String URL_UPDATE = URL_ENTITY + "/update";
    public static final String URL_ADD_UPDATE_ROLE_BAT = URL_ENTITY + "/add_update_role_bat";
    public static final String URL_UPDATE_PASSWORD = URL_ENTITY + "/update_password";
    public static final String URL_DELETE = URL_ENTITY + "/delete";
    public static final String URL_MAIN_SEARCH = URL_ENTITY + "/main_search";
    public static final String URL_DB_SEARCH = URL_ENTITY+"/search";
	
    @RequestMapping(value=URL_DB_SEARCH,method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String searchData( SysConfigSearchCondition sc,HttpSession session){
		String msg="";
		List<SysConfig>data = sysConfigBo.searchPojos(sc);
		DataBaseUtil<SysConfig> db = new DataBaseUtil<SysConfig>();
		int totalCount=sysConfigBo.searchCnt(sc);
		if(data.size()==0){
			msg="{\"draw\":"+sc.getDraw()+",\"recordsTotal\":0,\"recordsFiltered\":0,\"data\": []}";
		}else{
			db.setTotalCount(totalCount);
			db.setRecordsFiltered(totalCount);
			db.setData(data);
			msg = GsonUtil.gson.toJson(db);
		}
		return msg;
	}
    @RequestMapping(value= URL_GOTO_MAIN_PAGE,method=RequestMethod.GET)
    public ModelAndView gotoMainPage(HttpSession session,SysConfigSearchCondition sc){
    	User userInfo = (User)session.getAttribute(CommonJspKeys.JspParam_SessionUserInfo);
    	ModelAndView modelAndView= new ModelAndView("/sysConfig/sysConfig_main");
		modelAndView.addObject(CommonJspKeys.JspParam_SessionUserInfo,userInfo);
		return modelAndView;
    }
    @SuppressWarnings("rawtypes")
	@RequestMapping(value=URL_ADD, method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
    @ResponseBody
	public Json add(SysConfigSearchCondition sc, HttpSession session) {
		Json json = new Json();
		try {
			sysConfigBo.add(sc);
			json.setSuccess(true);
			json.setReturnCode(0);
		} catch (ComRuntimeException e) {
			json = new Json(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... "
					+ e.getMessage());
		}
		return json;
	}
    @SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = URL_UPDATE, method = RequestMethod.POST)
	public Json update(SysConfigSearchCondition sc, HttpSession session) {
		Json json = new Json();
		try {
			sysConfigBo.update(sc);
			json.setSuccess(true);
			json.setReturnCode(0);
		} catch (ComRuntimeException e) {
			json = new Json(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... "
					+ e.getMessage());
		}
		return json;
	}
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = URL_DELETE, method = RequestMethod.POST)
	public Json delete(SysConfigSearchCondition sc,HttpServletRequest request) {
		Json json = new Json();
		try {
			sysConfigBo.del(sc);
			json.setSuccess(true);
			json.setReturnCode(0);
		} catch (ComRuntimeException e) {
			json = new Json(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... "
					+ e.getMessage());
		}
		return json;
	}
}
